import { RouteRecordRaw, Router } from "vue-router";
//@ts-ignore
// import useAppStore from "@/store/index"
import useAppStore from '../store/index'
import { filterRoute } from "@/utils/filterRouters";
export const useHandleRouter = (router: Router) => {
    //设置白名单,直接放行
    const writeLists = ["Login"];
    router.beforeEach(async (to, _from, next) => {
        if (writeLists.includes(to.name as string)) {
            next();
            return;
        }
        const appStore = useAppStore();

        //已经获取菜单路由直接放行
        if (appStore.menuList.length) {
            next()
            return;
        }
        //获取菜单路由列表
        try {
            await appStore.getInfo();
            //处理成符合vue路由格式的路由
            const routers = filterRoute(appStore.menuList);
            //循环添加路由到父路由Index下
            routers.forEach((route: RouteRecordRaw) => {
                router.addRoute("Index", route);
            });
            ////添加完路由需要重新执行一次路由跳转，否则会出现空白页面
            next({ ...to, replace: true });
        } catch (error) {
            //如果接口出错 比如token过期继续往下走
            next()
        }
    });
}